// Text of project CharEdit written on 11/20/95 at 4:28 PM
// Beginning of text file constants.f
/*
**      Newton Developer Technical Support Sample Code
**
**      CharEdit, an example of the "comb editor" protoCharEdit
**
**      by Bob Ebert & J. Christopher Bell, Newton Developer Technical Support
**
**      Copyright  1995 by Apple Computer, Inc.  All rights reserved.
**
**      You may incorporate this sample code into your applications without
**      restriction.  This sample code has been provided "AS IS" and the
**      responsibility for its operation is 100% yours.  You are not
**      permitted to modify and redistribute the source as "DTS Sample Code."
**      If you are going to re-distribute the source, we require that you
**      make it clear in the source that the code was descended from
**      Apple-provided sample code, but that you've made changes.
*/


constant kMyLetters := "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
constant kMyDigitsOrSpace  := " 0123456789";
constant kMyDigits  := "0123456789";
// End of text file constants.f
// Beginning of file CharEdit Layout

// Before Script for "CharEditExample"
// Copyright 1995 Apple Computer, Inc.  All rights reserved.


CharEditExample :=
    {title: "CharEdit",
     viewBounds: {left: 0, top: 0, right: 240, bottom: 320},
     viewFormat: 83951953,
     viewSetupFormScript:
       func()
       	begin
       		local b := GetAppParams();
       		constant kMaxWidth := 240;
       		constant kMaxHeight := 336;
       		
       		viewBounds := RelBounds(b.appAreaLeft, b.appAreaTop,
       				Min(b.appAreaWidth, kMaxWidth),
       				Min(b.appAreaHeight, kMaxHeight) );		
       		
       		whichField := CADL;
       		myEditor.text := clone(whichField.text);
       		myEditor.template := whichField.template;
       		
       		// If our first field had not figured out its template/text yet (e.g. our date field), you would call:
       		// AddProcrastinatedSend(kAppSymbol, self, 'SetupEditor, ['aDate], 0);
       		
       	end,
     SyncText:
       func()
       	begin
       		local doNotCopy;
       		
       		// ask the protoCharEdit for the new data, cleaning it up if necessary
       		local newStuff := Clone(myEditor:GetWordForDisplay());
       		
       		// if you use a localized template (you got it from getlocale()),
       		// you might need to check if it is valid before copying it to your app. 
       		// For instance, for dates you could get the value "1/33/95", which "fits" the 
       		// template structure but would fail in template.setupstring. 
       		// We would rather not copy a value that would fail later...
       
       		if whichField = aDate and not StringToDate(newStuff) then
       			doNotCopy := true;
       			
       		// An alternate strategy for this kind of validation might be to let each child
       		// validate the data itself, either via a SetValue-like method, or via a specific
       		// Valid check.  e.g. whichField:SetIfValid(newStuff) or 
       		// if whichField:Validate(newstuff) then SetValue(...)
       		
       		// Change the text that the user is viewing at the top (in the para views)
       		if not doNotCopy then
       			SetValue(whichField, 'text, newStuff);
       	end,
     whichField:
       nil		// holds the field being edited
       ,
     SetupEditor:
       func(field)
       begin
       	whichField := field; // Change our current setting
       
       	// get the new template
       	myEditor.template := whichField.template;
       
       	// get the new text (and make a copy so we don't worry about modifying the original in-line)
       	myEditor.text := clone(whichField.text);
       
       	myEditor:UseTextAndTemplate();	// actually change the text & template
       
       		// update our label containing the field type
       	SetValue(label, 'text, whichField.label);
       end,
     viewFlags: 5,
     declareSelf: 'base,
     debug: "CharEditExample",
     viewClass: 74
    };

_view000 := {_proto: @401};
AddStepForm(CharEditExample, _view000);

_view001 :=
    {viewBounds: {left: 0, top: -18, right: 33, bottom: 0},
     viewJustify: 144,
     ViewScroll2DScript:
       func(direction,extras)
       begin
       	myEditor:Scroll(if direction = 'left then -1 else 1);
       	Inherited:?viewScroll2DScript(direction,extras);
       end,
     _proto: @657
    };
AddStepForm(_view000, _view001);



clearButton :=
    {
     buttonClickScript:
       func()
       	begin
       		myEditor:DeleteText(myEditor.wordLeft, myEditor.wordRight);
       		// myEditor:SetNewWord("", nil);
       		// myEditor:useTextAndTemplate();
       	end,
     text: "Clear",
     viewBounds: {left: 6, top: 0, right: 46, bottom: 13}
     ,
     viewJustify: 8389734,
     debug: "clearButton",
     _proto: @226
    };
AddStepForm(_view000, clearButton);





_view002 := {_proto: @229};
AddStepForm(CharEditExample, _view002);



label :=
    {text: nil,
     viewBounds: {left: 88, top: 224, right: 160, bottom: 240},
     viewSetupFormScript:
       func()
       begin
       text := "" & whichField;
       end,
     viewJustify: 8388614,
     debug: "label",
     _proto: @218
    };
AddStepForm(CharEditExample, label);
StepDeclare(CharEditExample, label, 'label);



myEditor :=
    {viewBounds: {left: 9, top: 241, right: 231, bottom: 295},
     viewFormat: 336,
     DisplayExternal:
       func(needsDisplay)
       	begin
       		if kDebugOn then
       			begin
       				write("DisplayExternal called with ");
       				print(needsDisplay);
       			end;
       
       		base:SyncText();
       	end,
     cellWidth: 22,
     debug: "myEditor",
     _proto: @393
    };
AddStepForm(CharEditExample, myEditor);
StepDeclare(CharEditExample, myEditor, 'myEditor);



CADLLabel :=
    {text: "CADL",
     viewBounds: {left: 8, top: 32, right: 56, bottom: 56},
     viewJustify: 8388612
     ,
     debug: "CADLLabel",
     _proto: @218
    };
AddStepForm(CharEditExample, CADLLabel);



CADL :=
    {viewBounds: {left: 57, top: 33, right: 231, bottom: 55},
     viewFlags: 513,
     viewFont: simpleFont12,
     viewFormat: 337,
     viewLineSpacing: 20,
     viewClickScript:
       func(unit)
       	begin
       		base:SyncText();	// copy the CharEdit data back to the original field
       		base:SetupEditor(self);
       		
       		true; // Return true if click has been completely handled, nil otherwise
       	end,
     template:
       {
       	format: "01111111",
       	string: "        ",
       	filters: [kMyLetters, kMyDigits],
       },
     text: "A0123456";,
     label: "CADL",
     debug: "CADL",
     viewClass: 81
    };
AddStepForm(CharEditExample, CADL);
StepDeclare(CharEditExample, CADL, 'CADL);



DateLabel :=
    {text: "Date",
     viewBounds: {left: 8, top: 64, right: 56, bottom: 88},
     viewJustify: 8388612
     ,
     debug: "DateLabel",
     _proto: @218
    };
AddStepForm(CharEditExample, DateLabel);



aDate :=
    {viewBounds: {left: 57, top: 65, right: 231, bottom: 87},
     viewFlags: 513,
     viewFont: simpleFont12,
     viewFormat: 337,
     viewLineSpacing: 20,
     viewSetupFormScript:
       func()
       	begin
       		// get the localized version of the date editor. This will vary
       		// based not only on the ROM, but on the locale user configuration preference
       		// note that we make our own modifyable frame so that the filter can set self.whatever...
       		template := {_proto: getlocale().dateFilter};
       		text := shortdatestr(time(), 0);
       	end,
     viewClickScript:
       func(unit)
       	begin
       		base:SyncText();	// copy the CharEdit data back to the original field
       		base:SetupEditor(self);
       		
       		true; // Return true if click has been completely handled, nil otherwise
       	end,
     template: nil,
     text: nil,
     label: "Date",
     debug: "aDate",
     viewClass: 81
    };
AddStepForm(CharEditExample, aDate);
StepDeclare(CharEditExample, aDate, 'aDate);



AmountLabel :=
    {text: "Amount",
     viewBounds: {left: 8, top: 96, right: 56, bottom: 120},
     viewJustify: 8388612,
     debug: "AmountLabel",
     _proto: @218
    };
AddStepForm(CharEditExample, AmountLabel);



amount :=
    {viewBounds: {left: 57, top: 97, right: 231, bottom: 119},
     viewFlags: 513,
     viewFont: simpleFont12,
     viewFormat: 337,
     viewLineSpacing: 20,
     template:
       {
       	format:	"0000001",
       	string:	"      0",
       	filters: [kMyDigitsOrSpace, kMyDigits],
       
       	SetupString: func(str) begin
       		// remove separators if present "1,002" -> "1002"
       		str := SPrintObject(Floor(StringToNumber(str)));
       		// pad string to 7 digits
       		if StrLen(str) < 7 then
       			StrMunger(str,0,0,string,0,7-StrLen(str));
       		str;
       	end,
       
       	CleanupString: func(str) begin
       		// replace spaces with zeros
       		StrReplace(str, " ", "0", nil);
       		str := NumberStr(StringToNumber(str));	// trim leading 0's
       		self._parent:SetNewWord(:SetupString(str), nil);
       		self._parent:UseNewWord();
       		str;
       	end,
       };,
     viewClickScript:
       func(unit)
       	begin
       		base:SyncText();	// copy the CharEdit data back to the original field
       		base:SetupEditor(self);
       		
       		true; // Return true if click has been completely handled, nil otherwise
       	end,
     label: "Amount",
     value: 12,
     text: nil,
     viewSetupDoneScript:
       func()
       	self.text := NumberStr(value);,
     debug: "amount",
     viewClass: 81
    };
AddStepForm(CharEditExample, amount);



PhoneLabel :=
    {text: "Phone",
     viewBounds: {left: 8, top: 128, right: 56, bottom: 152},
     viewJustify: 8388612,
     debug: "PhoneLabel",
     _proto: @218
    };
AddStepForm(CharEditExample, PhoneLabel);



phone :=
    {viewBounds: {left: 57, top: 129, right: 231, bottom: 151},
     viewFlags: 513,
     viewFont: simpleFont12,
     viewFormat: 337,
     viewLineSpacing: 20,
     text: "767-1212",
     template: nil,
     viewClickScript:
       func(unit)
       	begin
       		base:SyncText();	// copy the CharEdit data back to the original field
       		base:SetupEditor(self);
       		
       		true; // Return true if click has been completely handled, nil otherwise
       	end,
     viewSetupFormScript:
       func()
       	begin
       		// get the localized version of the phone editor. This will vary
       		// based not only on the ROM, but on the locale user configuration preference
       		// note that we make our own modifyable frame so that the filter can set self.whatever...
       		template := {_proto: getlocale().phoneFilter};
       	end,
     label: "Phone",
     debug: "phone",
     viewClass: 81
    };
AddStepForm(CharEditExample, phone);
StepDeclare(CharEditExample, phone, 'phone);



socSecLabel :=
    {text: "Soc Sec",
     viewBounds: {left: 8, top: 160, right: 56, bottom: 184},
     viewJustify: 8388612,
     debug: "socSecLabel",
     _proto: @218
    };
AddStepForm(CharEditExample, socSecLabel);



socSec :=
    {viewBounds: {left: 57, top: 161, right: 231, bottom: 183},
     viewFlags: 513,
     viewFont: simpleFont12,
     viewFormat: 337,
     viewLineSpacing: 20,
     viewClickScript:
       func(unit)
       	begin
       		base:SyncText();	// copy the CharEdit data back to the original field
       		base:SetupEditor(self);
       		
       		true; // Return true if click has been completely handled, nil otherwise
       	end,
     template:
       {
       	format: "000_00_0000",
       	string: "   -  -    ",
       	filters: [kMyDigits],
       },
     text: "123-45-6789";,
     label: "Soc Sec",
     debug: "socSec",
     viewClass: 81
    };
AddStepForm(CharEditExample, socSec);
StepDeclare(CharEditExample, socSec, 'socSec);



freeFormLabel :=
    {text: "FreeForm",
     viewBounds: {left: 8, top: 192, right: 56, bottom: 216},
     viewJustify: 8388612,
     debug: "freeFormLabel",
     _proto: @218
    };
AddStepForm(CharEditExample, freeFormLabel);



freeForm :=
    {viewBounds: {left: 57, top: 193, right: 231, bottom: 215},
     viewFlags: 513,
     viewFont: simpleFont12,
     viewFormat: 337,
     viewLineSpacing: 20,
     text: "Anything you want",
     template:
       nil
       
       /*
       {
       	// string: "",
       	// filters: [],
       }
       */,
     viewClickScript:
       func(unit)
       	begin
       		base:SyncText();	// copy the CharEdit data back to the original field
       		base:SetupEditor(self);
       		
       		true; // Return true if click has been completely handled, nil otherwise
       	end,
     label: "FreeForm",
     debug: "freeForm",
     viewClass: 81
    };
AddStepForm(CharEditExample, freeForm);
StepDeclare(CharEditExample, freeForm, 'freeForm);




constant |layout_CharEdit Layout| := CharEditExample;
// End of file CharEdit Layout



